<?php
/**
 * Created by PhpStorm.
 * User: chen
 * Date: 18-6-9
 * Time: 上午10:31
 */

namespace App\Model\Common;


use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class Position extends BasePaginateModel
{
    protected $table = 'position';

    public $primaryKey = 'p_id';
    protected $hidden = ['created_at', 'updated_at'];
    protected $guarded = [];

    /**
     * 职务列表
     */
    public static function positionList($map =[], $pageLimit)
    {
        $list = DB::table('position')->where($map)->select('p_id', 'p_name', 'p_authories', 'created_at', 'updated_at')->orderBy('p_id', 'desc')->paginate($pageLimit);
        if (sizeof($list) == 0) {
            return false;
        }
        return $list;
    }

    /**
     * 添加职务
     */
    public static function addPosition($params)
    {
        $authories = self::pushAuthories($params['p_authories']);
        $params['p_authories'] = $authories;
        return self::create($params);
    }

    /**
     * 拼接权限字符串
     */
    public static function pushAuthories($data)
    {
        $authoriesArray = explode(',', $data);
        $arr = DB::table('nosplit_authority as na')
            ->leftJoin('nosplit_module as nm', 'na.module_id', '=', 'nm.id')
            ->whereIn('na.id', $authoriesArray)
            ->select(DB::raw('GROUP_CONCAT(h_nm.nm_type) as nm_type'), DB::raw('GROUP_CONCAT(h_na.module_id) as module_id'), DB::raw('GROUP_CONCAT(h_na.id) as id'))
            ->get();
        $type = explode(',', $arr[0]->nm_type);//模块类型
        $module_id = explode(',', $arr[0]->module_id);//模块ID
        $id = explode(',', $arr[0]->id);//权限ID
        $authories = '';
        foreach ($type as $k=>$v) {
            $authories .= $v.'|'.$module_id[$k].'|'.$id[$k].',';
        }
        return $authories;
    }

    /**
     * 修改职务
     */
    public static function editPosition($id, $p_name, $p_authories)
    {
        $time = date('Y-m-d H:i:s', time());
        if ($p_name != null) {
            return self::where('p_id', $id)->update([
                'p_name'      => $p_name,
                'updated_at'  => $time,
            ]);
        } elseif ($p_authories  != null) {
            $authories = self::pushAuthories($p_authories);
            $params['p_authories'] = $authories;
            return self::where('p_id', $id)->update([
                'p_authories' => $params['p_authories'],
                'updated_at'  => $time,
            ]);
        } else {
            return false;
        }

    }

    /**
     * 删除职务
     */
    public static function delPosition($pid, $bId)
    {
        $id = DB::table('staff')
            ->leftjoin('user', 'area_manager_id', '=', 'u_id')
            ->where('u_b_id', $bId)
            ->where('is_del', '<', 2)
            ->where('post', $pid)
            ->value('id');
        if ($id) {
            return -1;
        }
        $time = date('Y-m-d H:i:s', time());
        return self::where('p_id', $pid)->update([
            'p_status' => 2,
            'updated_at'  => $time,
        ]);
    }
    //添加员工的时候  获取该公司职务的下拉框
    public static function downPosition($bId)
    {
        $list = DB::table('position')->where('p_bid', $bId)->where('p_status', 1)->select('p_id', 'p_name')->get();
        return $list;
    }

    /**
     * 修改了职务权限以后   在批量修改该职务下面所有员工的权限
     */
    public static function editPositionDownUserPermission($id)
    {

        //职务下面所有员工的ID
        $userIdArr = DB::table('user')
            ->leftJoin('staff', 'u_id', '=', 'area_manager_id')
            ->where('post', $id)
            ->value(DB::raw('GROUP_CONCAT(u_id) as u_id'));
        if (!$userIdArr) {
            return -1;
        }
        $p_authories = DB::table('position')->where('p_id', $id)->value('p_authories');
        $userIdArr = explode(',', $userIdArr);
        return DB::table('user')->whereIn('u_id', $userIdArr)->update(['authories' => $p_authories, 'updated_at' => time()]);
    }
    /**
     * 根据职务ID  获取职务名称
     */
    public static function getNameForPid($pid)
    {
        return DB::table('position')->where('p_id', $pid)->value('p_name');
    }

}